package zengjin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import zengjin.exception.CustomException;
import zengjin.mapper.AdminMapper;
import zengjin.mapper.EmployerMapper;
import zengjin.mapper.ImageMapper;
import zengjin.mapper.JobseekerMapper;
import zengjin.model.en.Admin;
import zengjin.model.en.Employer;
import zengjin.model.en.Image;
import zengjin.model.en.Jobseeker;
import zengjin.model.vo.UserVO;
import zengjin.service.UserService;

/**
 * @author 19324
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private JobseekerMapper jobseekerMapper;

    @Resource
    private EmployerMapper employerMapper;

    @Resource
    private ImageMapper imageMapper;


    @Override
    public UserVO login(String account, String password, HttpServletRequest request) {
        //1.参数处理
        String encryptPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        UserVO userVO = new UserVO();
        //2.1校验是否是管理员
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("admin_account", account);
        adminQueryWrapper.eq("admin_password", encryptPassword);
        adminQueryWrapper.eq("admin_status", "正常");
        Admin admin = adminMapper.selectOne(adminQueryWrapper);
        if (admin != null) {
            if (!"正常".equals(admin.getAdminStatus())) {
                throw new CustomException("该用户已封禁");
            }
            //记录用户的登录状态
            request.getSession().setAttribute(admin.getAdminAccount(), admin);

            userVO.setUserId(String.valueOf(admin.getAdminId()));
            userVO.setUserName(admin.getAdminUsername());
            userVO.setAccount(admin.getAdminAccount());
            userVO.setUserRole("admin");
            userVO.setUserStatus(admin.getAdminStatus());
            //查询出头像
            QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
            imageQueryWrapper.eq("image_name", "admin_avatar");
            imageQueryWrapper.eq("image_name_id", admin.getAdminId());
            Image image = imageMapper.selectOne(imageQueryWrapper);
            if (image != null) {
                userVO.setUserAvatarUrl(image.getImageUrl());
            }
            return userVO;
        }
        //2.2校验是否是求职用户
        QueryWrapper<Jobseeker> jobseekerQueryWrapper = new QueryWrapper<>();
        jobseekerQueryWrapper.eq("jobseeker_account", account);
        jobseekerQueryWrapper.eq("jobseeker_password", encryptPassword);
        jobseekerQueryWrapper.eq("jobseeker_status", "正常");
        Jobseeker jobseeker = jobseekerMapper.selectOne(jobseekerQueryWrapper);
        if (jobseeker != null) {
            if (!"正常".equals(jobseeker.getJobseekerStatus())) {
                throw new CustomException("该用户已封禁");
            }
            //记录用户的登录状态
            request.getSession().setAttribute(jobseeker.getJobseekerAccount(), jobseeker);

            userVO.setUserId(String.valueOf(jobseeker.getJobseekerId()));
            userVO.setUserName(jobseeker.getJobseekerUsername());
            userVO.setAccount(jobseeker.getJobseekerAccount());
            userVO.setUserRole("jobseeker");
            userVO.setUserStatus(jobseeker.getJobseekerStatus());
            userVO.setUserIsVerified(jobseeker.getJobseekerIsVerified());
            //查询出头像
            QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
            imageQueryWrapper.eq("image_name", "jobseeker_avatar");
            imageQueryWrapper.eq("image_name_id", jobseeker.getJobseekerId());
            Image image = imageMapper.selectOne(imageQueryWrapper);
            if (image != null) {
                userVO.setUserAvatarUrl(image.getImageUrl());
            }
            return userVO;
        }
        //2.3校验是否是企业用户
        QueryWrapper<Employer> employerQueryWrapper = new QueryWrapper<>();
        employerQueryWrapper.eq("employer_account", account);
        employerQueryWrapper.eq("employer_password", encryptPassword);
        Employer employer = employerMapper.selectOne(employerQueryWrapper);
        if (employer != null) {
            if (!"正常".equals(employer.getEmployerStatus())) {
                throw new CustomException("该用户已封禁");
            }
            //记录用户的登录状态
            request.getSession().setAttribute(employer.getEmployerAccount(), employer);

            userVO.setUserId(String.valueOf(employer.getEmployerId()));
            userVO.setUserName(employer.getEmployerCompanyName());
            userVO.setAccount(employer.getEmployerAccount());
            userVO.setUserRole("employer");
            userVO.setUserStatus(employer.getEmployerStatus());
            userVO.setUserIsVerified(employer.getEmployerIsVerified());
            //查询出头像
            QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
            imageQueryWrapper.eq("image_name", "employer_avatar");
            imageQueryWrapper.eq("image_name_id", employer.getEmployerId());
            Image image = imageMapper.selectOne(imageQueryWrapper);
            if (image != null) {
                userVO.setUserAvatarUrl(image.getImageUrl());
            }
            return userVO;
        }
        return null;
    }

    @Override
    public Boolean register(String account, String password, String role) {
        //1.参数处理
        String encryptPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        //查询求职者是否有该用户
        QueryWrapper<Jobseeker> jobseekerQueryWrapper = new QueryWrapper<>();
        jobseekerQueryWrapper.eq("jobseeker_account", account);
        if (jobseekerMapper.selectOne(jobseekerQueryWrapper) != null) {
            throw new CustomException("该用户已存在");
        }
        //查询企业是否有该用户
        QueryWrapper<Employer> employerQueryWrapper = new QueryWrapper<>();
        employerQueryWrapper.eq("employer_account", account);
        if (employerMapper.selectOne(employerQueryWrapper) != null) {
            throw new CustomException("该用户已存在");
        }
        //2.1注册求职用户
        if ("jobseeker".equals(role)) {
            Jobseeker jobseeker = new Jobseeker();
            jobseeker.setJobseekerStatus("正常");
            jobseeker.setJobseekerIsVerified("未认证");
            jobseeker.setJobseekerAccount(account);
            jobseeker.setJobseekerPassword(encryptPassword);
            jobseekerMapper.insert(jobseeker);
            //插入头像
            Image image = new Image();
            image.setImageName("jobseeker_avatar");
            image.setImageNameId(jobseeker.getJobseekerId());
            imageMapper.insert(image);
            return true;
        }
        //2.2注册企业用户
        else if ("employer".equals(role)) {
            Employer employer = new Employer();
            employer.setEmployerStatus("正常");
            employer.setEmployerIsVerified("未认证");
            employer.setEmployerAccount(account);
            employer.setEmployerPassword(encryptPassword);
            employerMapper.insert(employer);
            //插入头像
            Image image = new Image();
            image.setImageName("employer_avatar");
            image.setImageNameId(employer.getEmployerId());
            imageMapper.insert(image);
            return true;
        }
        else if("admin".equals(role)){
            Admin admin = new Admin();
            admin.setAdminStatus("正常");
            admin.setAdminAccount(account);
            admin.setAdminPassword(encryptPassword);
            admin.setAdminRole("超级管理员");
            adminMapper.insert(admin);
            //插入头像
            Image image = new Image();
            image.setImageName("admin_avatar");
            image.setImageNameId(admin.getAdminId());
            imageMapper.insert(image);
            return true;
        }
        return false;
    }

    @Override
    public Boolean logout(HttpServletRequest request, String account) {
        //1.判断是否登录
        if (request.getSession().getAttribute(account) == null) {
            throw new CustomException("未登录");
        }
        //2.注销
        request.getSession().removeAttribute(account);
        return true;
    }

    @Override
    public boolean resetPassword(String account, String password, String role) {
        //1.参数处理，
        String encryptPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.判断身份
        if ("jobseeker".equals(role)) {
            //2.1求职者
            LambdaUpdateWrapper<Jobseeker> jobseekerLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            jobseekerLambdaUpdateWrapper.eq(Jobseeker::getJobseekerAccount, account)
                    .set(Jobseeker::getJobseekerPassword, encryptPassword);
            return jobseekerMapper.update(null, jobseekerLambdaUpdateWrapper) > 0;
        } else if ("employer".equals(role)) {
            //2.2企业
            LambdaUpdateWrapper<Employer> employerLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            employerLambdaUpdateWrapper.eq(Employer::getEmployerAccount, account)
                    .set(Employer::getEmployerPassword, encryptPassword);
            return employerMapper.update(null, employerLambdaUpdateWrapper) > 0;
        } else if ("admin".equals(role)) {
            //2.3管理员
            LambdaUpdateWrapper<Admin> adminLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            adminLambdaUpdateWrapper.eq(Admin::getAdminAccount, account)
                    .set(Admin::getAdminPassword, encryptPassword);
            return adminMapper.update(null, adminLambdaUpdateWrapper) > 0;
        }
        return false;
    }
}
